const dbpool=require("../config/dbpoolconfig");

const userController={
    login(req,res){
        let email = req.body.email;
        let pwd = req.body.pwd;
        dbpool.connect('select * from user where email=? and pwd=?',[''+email+'',''+pwd+''],(err,data)=>{
            if(!err){
                if(data.length > 0){
                    res.redirect('index.html');
                }else{
                    res.send('邮箱或密码错误！')
                }
            }
        })
    },
    register(req,res){
        let username = req.body.username;
        let email = req.body.email;
        let pwd = req.body.pwd;
        dbpool.connect('insert into user values(?,?,?,?)',[null,''+username+'',''+email+'',''+pwd+''],(err,data)=>{
            if(!err){
                dbpool.connect('select * from user where username=? or email=?',[''+username+'',''+email+''],(err,data)=>{
                    if(!err){
                        console.log(data.length);
                        if(data.length > 1){
                            res.send('输入错误！');
                        }else{
                            res.redirect('signIn.html');
                        }
                    }
                })
            }
        })
    },
    checkUsername(req,res){
        let username = req.query.username;
        dbpool.connect('select * from user where username=?',[''+username+''],(err,data)=>{
            if(!err){
                if(data.length > 0){
                    res.send('用户名已存在！')
                }else{
                    res.send('用户名正确')
                }
            }
        })
    },
    checkEmail(req,res){
        let email = req.query.email;
        dbpool.connect('select * from user where email=?',[''+email+''],(err,data)=>{
            if(!err){
                if(data.length > 0){
                    res.send('邮箱已存在！')
                }else{
                    res.send('邮箱正确')
                }
            }
        })
    }
}
module.exports=userController;